package com.lhy.skywalking;

import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.matcher.ElementMatchers;

import java.lang.instrument.Instrumentation;

public class AgentMethod {
    public static void premain(String args, Instrumentation instrumentation) {
        System.out.println("-----------------准备开始代理----------------");
        System.out.println("-----------------代理参数：" + args);
        AgentBuilder.Transformer transformer = (builder,typeDescription, classLoader, javaModule) -> {
            // 对所有的方法进行拦截
            return builder.method(ElementMatchers.any())
                    .intercept(MethodDelegation.to(RequestTimeInterceptor.class));
        };

        // 拦截指定包下的方法
        new AgentBuilder.Default().type(ElementMatchers.nameStartsWith("com.lhy.skywalking.demo"))
                .transform(transformer).installOn(instrumentation);
    }
}
